package com.we823.cms.controller.front;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.PageInfo;
import com.we823.cms.common.web.pageable.Direction;
import com.we823.cms.common.web.pageable.PageRequest;
import com.we823.cms.common.web.pageable.Sort;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.we823.cms.common.WebConstants;
import com.we823.cms.common.utils.RequestUtil;
import com.we823.cms.common.web.FrontUtils;
import com.we823.cms.model.cms.NewsChannel;
import com.we823.cms.model.cms.News;
import com.we823.cms.repository.service.cms.NewsChannelService;
import com.we823.cms.repository.service.cms.NewsService;

@Controller
public class NewsController {

	private String baseUrl = WebConstants.WEB_TEMPLATE_PREFIX;
	private @Resource NewsService newsService;
	private @Resource NewsChannelService newsChannelService;

	@RequestMapping("newss")
	public String newss(HttpServletRequest request, Model model) {
		Integer channelId = RequestUtil.getValueByName(request, "id", -1);
		boolean pageable = RequestUtil.getPageable(request);
		Integer page = RequestUtil.getValueByName(request, "p", 0);
		Integer pageSize = RequestUtil.getValueByName(request, "s", 15);
		List<News> newsModels = null;
		if (channelId > 0) {
			if (pageable) {
				PageRequest pageRequest = new PageRequest(page, pageSize, Direction.DESC, "addtime");
				PageInfo<News> newsPage = newsService.findByNewsChannel(channelId, pageRequest);
				if (newsPage != null) {
					newsModels = newsPage.getList();
				}
			} else {
				newsModels = newsService.findByChannel(channelId, new Sort(Direction.DESC, "addtime"));
			}
		} else {
			if (pageable) {
				PageRequest pageRequest = new PageRequest(page, pageSize, Direction.DESC, "addtime");
				PageInfo<News> newsPage = newsService.findAll(pageRequest);
				if (newsPage != null) {
					newsModels = newsPage.getList();
				}
			} else {
				newsModels = newsService.findAll(new Sort(Direction.DESC, "addtime"));
			}
		}
		model.addAttribute("newsModels",newsModels);
		
		if(channelId>0){
			NewsChannel newsChannelModel = newsChannelService.findOne(channelId);
			if(newsChannelModel!=null){
				model.addAttribute("channelName",newsChannelModel.getName());
			}else{
				model.addAttribute("channelName","错误栏目信息");
			}
		}else{
			model.addAttribute("channelName","所有新闻");
		}
		
		List<NewsChannel> newsChannelModels = newsChannelService.findAll();
		model.addAttribute("newsChannels",newsChannelModels);
		
		return FrontUtils.buildTemplateUrl(baseUrl, "newss");
	}

	@RequestMapping("news_{id}")
	public String news(@PathVariable("id") Integer id, Model model) {
		News newsModel = newsService.findOne(id);

		newsModel.setHits(newsModel.getHits()+1);
		newsService.edit(newsModel, newsModel.getId());
		model.addAttribute("news", newsModel);

		NewsChannel newsChannel = newsChannelService.findOne(newsModel.getNewsChannelId());
		model.addAttribute("newsChannel", newsChannel);
		return FrontUtils.buildTemplateUrl(baseUrl, "news");
	}
}
